package cn.javaxc.mvcwork.controller;
import cn.javaxc.mvcwork.controller.SystemStatus;
import cn.javaxc.mvcwork.framework.database.*;
import java.sql.*;
import java.util.*;
import javax.servlet.*;
import org.apache.log4j.Logger;

import cn.javaxc.mvcwork.util.common.DatabaseUtils;
import org.springframework.stereotype.Component;
import org.springframework.web.context.ServletContextAware;
import org.springframework.context.annotation.DependsOn;
import org.springframework.beans.factory.annotation.Autowired;

@DependsOn("mvcConfig")
@Component
public class SystemStatusApp extends SystemStatus implements ServletContextAware
{
  
  private final static Logger log = Logger.getLogger(SystemStatusApp.class);

  public final static String fs = System.getProperty("file.separator");
  private SystemStatus thisSystem;
	@Autowired
	freemarker.template.Configuration freeMarkerConfiguration;
  
  public SystemStatusApp() {
  }



  
  public void setServletContext(ServletContext context){

    log.info("Initializing");
//	OgnlListener.setMethodAccessor();
    //Start the ConnectionPoolImpl with default params, these can be adjusted
    //in the InitHook

    //All virtual hosts will have an entry in SystemStatus, so this needs
    //to be reset when the context is reset
	thisSystem = SecurityHook.createSystemStatus(context,DatabaseUtils.createConnectionElement(),null) ;
//	thisSystem.setSessionFactory(getSessionFactory());
	thisSystem.setSqlSessionFactory(getSqlSessionFactory());
	thisSystem.setBaseDAO(getBaseDAO());
	try{
		  thisSystem.queryStatus(context);
		 freeMarkerConfiguration.setSharedVariable("APP_ORGANIZATION",context.getAttribute("APP_ORGANIZATION"));
      } catch (Exception e) {
		  e.printStackTrace();
      }
	context.setAttribute("SystemStatus", thisSystem);
	context.setAttribute("template",new cn.javaxc.mvcwork.modules.login.TemplateBean());

    log.info("Initialized");
  }


  
//  public void destroy() {
//    log.debug("Shutting down");
//    
//    //Remove the SystemStatus -> this will force a rebuild of any systems that
//    //may have been cached
////    Hashtable systemStatusList = (Hashtable) context.getAttribute("SystemStatus");
//    if (thisSystem != null) {
//      thisSystem.stopServers();
//    }
//
//    
//    log.debug("Shutdown complete");
//  }
}
